package sg.edu.nus.iss.team2s.jass.dao;

/**
 * @author Zhang Jinzhong
 */

import java.io.IOException;
import java.util.List;

import sg.edu.nus.iss.team2s.jass.util.PropertiesUtil;

public abstract class AbstractPropertiesDAO implements DAOInterface {

	public abstract void add(Object obj) throws DAOException;

	public abstract Object get(String key) throws DAOException;

	public abstract List<Object> list() throws DAOException;

	public abstract void update(List<Object> list) throws DAOException;
	
	//get the fields, need to be overridden in the subclass
//	protected String[] getFields(){
//		return new String[]{"id","name","branchId"};
//	}
	
	//flush objects to properties file
	protected void flush(List<Object> objects, String fileName) throws DAOException{
//		String[] fields = getFields();
		try {
			PropertiesUtil propUtil = PropertiesUtil.getInstance();
			propUtil.persist(fileName, objects);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new DAOException("IOException", e);
		}
		catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new DAOException("UnExpected Exception", e);
		}
		
	}
	
	//retrieve objects from properties file
	protected List<Object> retrieve(String fileName) throws DAOException{
//		String[] fields = getFields();
		List<Object> objects = null;
		PropertiesUtil propUtil;
		try {
			propUtil = PropertiesUtil.getInstance();
			objects = propUtil.retrieve(fileName);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new DAOException("IOException", e);
		}catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new DAOException("UnExpected Exception", e);
		}
		return objects;
	}
}
